package genomemap.util;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Calendar;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author Susanta Tewari
 */
public class AbstractJob {

    protected static DecimalFormat decimalFormat = new DecimalFormat("0.####");

    /**
     * a write-only bound property
     */
    public static final String PROP_MESSAGE = "message";

    /**
     * a write-only bound property.
     */
    public static final String PROP_PROGRESS = "progress";

    protected PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    /**
     *
     */
    private String message = "";

    /**
     *
     */
    private int progress = 0;

    /**
     * Set the value of message
     *
     * @param message new value of message
     */
    public void setMessage(String message) {
        String oldMessage = this.message;
        this.message = message;
        propertyChangeSupport.firePropertyChange(PROP_MESSAGE, oldMessage, message);
    }

    /**
     * Set the value of progress
     *
     * @param progress new value of progress
     */
    public void setProgress(int progress) {
        int oldProgress = this.progress;
        this.progress = progress;
        propertyChangeSupport.firePropertyChange(PROP_PROGRESS, oldProgress, progress);
    }

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    public Element getTagElement() {
        return DocumentHelper.createElement(getClass().getName());
    }

    protected Element createInput() {
        setMessage("creating XML input");
        Element inputElement = DocumentHelper.createElement("input");
        return inputElement;
    }

    protected Element createOutput() {
        setMessage("creating XML output");
        Element outputElement = DocumentHelper.createElement("output");
        return outputElement;
    }

    public Element createXMLOutput() {

        Element input = createInput();
        Element output = createOutput();

        setMessage("assembling XML result");
        Element element = getTagElement();
        element.addAttribute("time", Calendar.getInstance().getTime().toString());
        element.add(input);
        element.add(output);
        return element;
    }

    public void printXMLOutput() {

        Element output = createXMLOutput();

        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = null;

        try {
            setMessage("printing XML result");
            writer = new XMLWriter(System.out, format);
            writer.write(output);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

}
